#ifndef __PORTB_H
#define __PORTB_H
#include <pconfig.h>
#include "p18cxxx.h"
/* PIC18 PORTB library interface */

/* PORTB Config Bit Defines */
//----------------AND OR MASK-------------------------------------
#ifdef USE_OR_MASKS
#define PORTB_CHANGE_INT_ON   	0b10000000  /* PORTB interrupts on  */
#define PORTB_CHANGE_INT_OFF  	0b00000000  /* PORTB interrupts off */
#define PORTB_CHANGE_INT_MASK	(~PORTB_CHANGE_INT_ON)

#define PORTB_PULLUPS_ON      	0b00000000  /* PORTB pullups on     */
#define PORTB_PULLUPS_OFF     	0b00000001  /* PORTB pullups off    */
#define PORTB_PULLUPS_MASK		(~PORTB_PULLUPS_OFF)

#define RISING_EDGE_INT  		0b00000010 /*Interrupt is set by a rising edge  */
#define FALLING_EDGE_INT 		0b00000000 /*Interrupt is set by a falling edge */
#define EDGE_INT_MASK    		(~RISING_EDGE_INT)

#define PORTB_INT_PRIO_HIGH     0b01000000 /*Interrupt priority high*/
#define PORTB_INT_PRIO_LOW      0b00000000 /*Interrupt priority low*/
#define PORTB_INT_PRIO_MASK     (~PORTB_INT_PRIO_HIGH) 

//-------------------AND MASK-------------------------------------

#else
#define PORTB_CHANGE_INT_ON   	0b11111111  /* PORTB interrupts on  */
#define PORTB_CHANGE_INT_OFF 	0b01111111  /* PORTB interrupts off */
#define PORTB_PULLUPS_ON      	0b11111110  /* PORTB pullups on     */
#define PORTB_PULLUPS_OFF     	0b11111111  /* PORTB pullups off    */
#define RISING_EDGE_INT  		0b11111111 /*Interrupt is set by a rising edge  */
#define FALLING_EDGE_INT 		0b11111101 /*Interrupt is set by a falling edge */
#define PORTB_INT_PRIO_HIGH     0b11111111 /*Interrupt priority high*/
#define PORTB_INT_PRIO_LOW      0b10111111 /*Interrupt priority low*/
#endif

/* PORTB Function Prototypes */
#define PARAM_SCLASS auto

#if defined (PTB_V5)
//TODO  remove once V5 header files are changed
//#define RABIE RBIE
/* EnablePullups
 * Enable PORTB resistors
 */
#define EnablePullups() INTCON2bits.RABPU=0

/* DisablePullups
 * Disable PORTB resistors
 */
#define DisablePullups() INTCON2bits.RABPU=1

#else
/* EnablePullups
 * Enable PORTB resistors
 */
#define EnablePullups() INTCON2bits.RBPU=0

/* DisablePullups
 * Disable PORTB resistors
 */
#define DisablePullups() INTCON2bits.RBPU=1
#endif

/* OpenPORTB
 * Configure PORTB interrupts, resistors
 */
void OpenPORTB(PARAM_SCLASS unsigned char);

#if defined (PTB_V5)

/* ClosePORTB
 * Disable PORTB interrupts, resistors
 */
#define ClosePORTB() INTCONbits.RABIE=0, DisablePullups()

#else


/* ClosePORTB
 * Disable PORTB interrupts, resistors
 */
#define ClosePORTB() INTCONbits.RBIE=0, DisablePullups()

#endif

/* OpenRB0INT
 * Enable RB0INT interrupts, falling edge/rising edge
 */
void OpenRB0INT(PARAM_SCLASS unsigned char);

/* CloseRB0INT
 * Disable RB0INT interrupts, resistors
 */
#define CloseRB0INT() INTCONbits.INT0IE=0

/* OpenRB1INT
 * Enable RB1INT interrupts, falling edge/rising edge
 */
void OpenRB1INT(PARAM_SCLASS unsigned char);

/* CloseRB1INT
 * Disable RB1INT interrupts, resistors
 */
#define CloseRB1INT() INTCON3bits.INT1IE=0

/* OpenRB2INT
 * Enable RB2INT interrupts, falling edge/rising edge
 */
void OpenRB2INT(PARAM_SCLASS unsigned char);

/* CloseRB2INT
 * Disable RB2INT interrupts, resistors
 */
#define CloseRB2INT() INTCON3bits.INT2IE=0

#if defined PTB_V2 || defined PTB_V3 || defined PTB_V6

/* OpenRB3INT
 * Enable RB3INT interrupts, falling edge/rising edge
 */
void OpenRB3INT(PARAM_SCLASS unsigned char);

/* CloseRB3INT
 * Disable RB3INT interrupts, resistors
 */
#define CloseRB3INT() INTCON3bits.INT3IE=0

#endif

#endif
